package com.ql.controller.user;

import com.alibaba.fastjson.JSON;
import com.ql.pojo.user.Keshi;
import com.ql.pojo.user.Role;
import com.ql.pojo.yk.Doctor;
import com.ql.service.doctor.DoctorService;
import com.ql.service.keshi.KeShiService;
import com.ql.service.role.RoleService;
import com.ql.service.user.UserService;
import com.utils.FastDFSClient;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private DoctorService doctorService;

    @Resource
    private KeShiService keShiService;
    @Resource
    private RoleService roleService;

    @RequestMapping("/login")
    public String login(String u, String p, HttpSession session){
        Map map =  new HashMap();
        Doctor doctor = userService.login(u);
        UsernamePasswordToken token = new UsernamePasswordToken(u,p);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            session.setAttribute("doctor",doctor);
            return "index";
        } catch (Exception e) {
            map.put("msg","用户名或密码错误");
        }

        return "redirect:http://localhost:8080";
    }

    @RequestMapping(value = {"/photoUpload"},method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String photoUpload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        Map<String,Object> map = new HashMap<>();
        try {
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:client.conf");
            String path = fastDFSClient.uploadFile(file.getBytes(), extName);
            String url = "http://192.168.25.150/"+path;
            map.put("photo",url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.toJSON(map).toString();
    }

    @RequestMapping(value = {"/userUpdate"},method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String userUpdate(Doctor doctor,HttpSession session){
        Map<String,Object> map = new HashMap<>();
        Doctor doctor2 = (Doctor) session.getAttribute("doctor");
        if(null!=doctor){
            if(doctor.getDocsex()!=null){
                doctor2.setDocsex(doctor.getDocsex());
            }
            if(doctor.getPhone()!=null){
                doctor2.setPhone(doctor.getPhone());
            }
            if(doctor.getDepartment()!=null){
                doctor2.setDepartment(doctor.getDepartment());
            }
            if(doctor.getJobtitle()!=null){
                doctor2.setJobtitle(doctor.getJobtitle());
            }
            if(doctor.getPhone()!=null){
                doctor2.setPhoto(doctor.getPhoto());
            }
        }
        doctorService.xiugaiDoctor(doctor2);
        map.put("msg","修改成功");
        session.setAttribute("doctor",doctor2);
        return JSON.toJSONString(map);
    }


    @RequestMapping(value = {"/findKeshi"},method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String findKeshi(HttpSession session){
        List<Keshi> keShi = keShiService.findKeShi();
        Doctor doctor = (Doctor) session.getAttribute("doctor");
        Map<String,Object> map = new HashMap<>();
        map.put("data",keShi);
        map.put("depa",doctor.getDepartment());
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = {"/findRole"},method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String findRole(HttpSession session){
        List<Role> role = roleService.findRole();
        Doctor doctor = (Doctor) session.getAttribute("doctor");
        Map<String,Object> map = new HashMap<>();
        map.put("roles",role);
        map.put("roleId",doctor.getJobtitle());
        return JSON.toJSONString(map);
    }
}
